package com.vcg.community.web.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;
import org.apache.log4j.Logger;

import vc.bean.SearchHits;
import vc.dubbo.result.DubboResult;
import vc.search.service.ResourceService;

import com.vcg.community.service.FavoriteService;
import com.vcg.community.service.GroupGetService;
import com.vcg.community.service.UserGetService;
import com.vcg.community.service.VoteService;
import com.vcg.community.model.Resource;
import com.vcg.community.model.User;
import com.vcg.community.model.UserGroup;

public class HelloWorldController implements Controller {
	
	@Autowired
	FavoriteService fService;
	@Autowired
	VoteService voteService;
	@Autowired
	UserGetService userService;
	@Autowired
	GroupGetService groupService;
	@Autowired
	ResourceService resourceService;

	
	Logger logger=Logger.getLogger(HelloWorldController.class);
	 public ModelAndView handleRequest(HttpServletRequest req, HttpServletResponse resp) throws Exception {  
		 
		 StringBuilder strMessageBuilder=new StringBuilder();
		 strMessageBuilder.append("用户");
		 Integer iD=1;
		 if(!"".equals(req.getParameter("userId"))){
			 iD= Integer.parseInt(req.getParameter("userId"));			 
		 }
		 strMessageBuilder.append(iD);

	   //1、收集参数、验证参数  
       //2、绑定参数到命令对象  
       //3、将命令对象传入业务对象进行业务处理  
       //4、选择下一个页面  
       ModelAndView mv = new ModelAndView();  
       //设置逻辑视图名，视图解析器会根据该名字解析到具体的视图页面  
       if("favorited".equals(req.getParameter("action"))){
  		   strMessageBuilder.append("的收藏列表如下：");
    	   DubboResult<List<Resource>> dbResult=fService.getFavoritedResources(iD);
    	   mv.addObject("resultList", dbResult.getResult()); 
       }else if("liked".equals(req.getParameter("action"))){
  		       strMessageBuilder.append("的点赞列表如下：");
        	   DubboResult<List<Resource>> dbResult=voteService.getVotedResources(iD);
        	   mv.addObject("resultList", dbResult.getResult()); 
       }else if("friends".equals(req.getParameter("action"))){
		   strMessageBuilder.append("的关注列表如下：");
    	   DubboResult<List<User>> dbResult=userService.getFriends(iD);
    	   mv.addObject("resultList", dbResult.getResult()); 
       }else if("followers".equals(req.getParameter("action"))){
		   strMessageBuilder.append("的粉丝列表如下：");
    	   DubboResult<List<User>> dbResult=userService.getFollowers(iD);
    	   mv.addObject("resultList", dbResult.getResult()); 
       }else if("groups".equals(req.getParameter("action"))){
		   strMessageBuilder.append("的群组列表如下：");
    	   DubboResult<List<UserGroup>> dbResult=groupService.getUserGroups(iD);
    	   mv.addObject("resultList", dbResult.getResult()); 
       }else if("search".equals(req.getParameter("action"))){
		   strMessageBuilder.append("搜索结果如下：");
    	   DubboResult<SearchHits> dbResult=resourceService.search("select * from index.resource");
    	   mv.addObject("resultList", dbResult.getResult()); 
       }
       //       logger.error("Get "+dbResult.getResult() + " Objects!");
       //添加模型数据 可以是任意的POJO对象  
       mv.addObject("message", strMessageBuilder.toString());  
       
       mv.setViewName("hello");  
       return mv;  
    }  
}
